<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Module: ActiveBackground::DatabaseHandler</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />

    <script language="JavaScript" type="text/javascript">
    // <![CDATA[

        function toggleSource( id )
        {
          var elem
          var link

          if( document.getElementById )
          {
            elem = document.getElementById( id )
            link = document.getElementById( "l_" + id )
          }
          else if ( document.all )
          {
            elem = eval( "document.all." + id )
            link = eval( "document.all.l_" + id )
          }
          else
            return false;

          if( elem.style.display == "block" )
          {
            elem.style.display = "none"
            link.innerHTML = "show source"
          }
          else
          {
            elem.style.display = "block"
            link.innerHTML = "hide source"
          }
        }

        function openCode( url )
        {
          window.open( url, "SOURCE_CODE", "width=400,height=400,scrollbars=yes" )
        }
      // ]]>
    </script>

  </head>

  <body>
  <table width="100%" border='0' cellpadding='0' cellspacing='0' class='banner'><tr>
  <td class="file-title"><span class="file-title-prefix">Module</span><br />ActiveBackground::DatabaseHandler</td>
  <td align="right">
    <table cellspacing=0 cellpadding=2>
      <tr valign="top">
        <td>In:</td>
        <td>

<a href="../../files/vendor/plugins/active_background/tasks/__/lib/active_background/database_rb.html">vendor/plugins/active_background/tasks/../lib/active_background/database.rb</a>
        </td>
      </tr>
         </table>
        </td>
        </tr>
      </table>
 <!-- banner header -->

  <div id="bodyContent">
      <div id="content">




  <div class="sectiontitle">Methods</div>
  <ul>
  <li><a href="#M000001">database</a></li>
  </ul>








<div class="sectiontitle">Public Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000001"></a><b>database</b>(attempt=0, &amp;block)
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000001_source')" id="l_M000001_source">show source</a> ]</p>

  <div id="M000001_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 3</span>
 3:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">database</span>(<span class="ruby-identifier">attempt</span>=<span class="ruby-value">0</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
 4:       <span class="ruby-identifier">database</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">Database</span>.<span class="ruby-identifier">instance</span>
 5:       <span class="ruby-identifier">database</span>.<span class="ruby-identifier">lock</span>
 6:           <span class="ruby-comment cmt"># TODO: Optimize code and remove double checks</span>
 7:       <span class="ruby-keyword kw">begin</span>
 8:         <span class="ruby-identifier">db</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">do</span>
 9:           <span class="ruby-keyword kw">begin</span>
10:             <span class="ruby-identifier">database</span>.<span class="ruby-identifier">prepare</span>
11:             <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">instance_eval</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
12:             <span class="ruby-identifier">database</span>.<span class="ruby-identifier">terminate</span>
13:           <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
14:             <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">error</span>(<span class="ruby-value str">&quot;\n******************************************\n&quot;</span> <span class="ruby-operator">+</span>
15:                           <span class="ruby-node">&quot;Exception: #{e.to_s} - (#{e.class})\n&quot;</span> <span class="ruby-operator">+</span>
16:                           <span class="ruby-value str">&quot;Database: Failed connecting to database &quot;</span> <span class="ruby-operator">+</span> (<span class="ruby-identifier">attempt</span><span class="ruby-operator">&lt;=</span><span class="ruby-value">10</span><span class="ruby-operator">?</span><span class="ruby-node">&quot;(attempt: #{attempt})\n&quot;</span><span class="ruby-operator">:</span><span class="ruby-value str">&quot;Action killed\n&quot;</span>) <span class="ruby-operator">+</span>
17:                           <span class="ruby-value str">&quot;----------------BACKTRACE-----------------\n&quot;</span> <span class="ruby-operator">+</span>
18:                                     <span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;\n&quot;</span> <span class="ruby-operator">+</span>
19:                           <span class="ruby-value str">&quot;-------------------END--------------------\n&quot;</span> <span class="ruby-operator">+</span>
20:                           <span class="ruby-value str">&quot;\n******************************************\n&quot;</span> )
21:             <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">error</span>(<span class="ruby-node">&quot;Database: Action failed with #{block.inspect}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">attempt</span><span class="ruby-operator">&lt;=</span><span class="ruby-value">10</span>
22:             <span class="ruby-identifier">database</span>(<span class="ruby-identifier">attempt</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attempt</span> <span class="ruby-operator">&lt;=</span><span class="ruby-value">10</span>
23:           <span class="ruby-keyword kw">end</span>
24:         <span class="ruby-keyword kw">end</span>
25:         <span class="ruby-identifier">db</span>.<span class="ruby-identifier">join</span>
26:       <span class="ruby-keyword kw">rescue</span>
27:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attempt</span> <span class="ruby-operator">&lt;=</span><span class="ruby-value">10</span>
28:           <span class="ruby-identifier">database</span>(<span class="ruby-identifier">attempt</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
29:           <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">debug</span> <span class="ruby-node">&quot;Database: Failed to complete task, attempting (#{attempt}) again&quot;</span>
30:         <span class="ruby-keyword kw">else</span>
31:           <span class="ruby-identifier">raise</span> <span class="ruby-value str">'Database: Could not complete database task for job '</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">key</span>
32:           <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">warn</span> <span class="ruby-value str">'Database: Could not complete database task for job '</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">key</span>
33:         <span class="ruby-keyword kw">end</span>
34:       <span class="ruby-keyword kw">end</span>
35:       <span class="ruby-identifier">database</span>.<span class="ruby-identifier">unlock</span>
36:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
</div>

  </div>

    </body>
</html>